using System.Linq;
using AvalonDock.Layout;
using Sany.AGV.RobotTool.ViewModels;

namespace Sany.AGV.RobotTool
{
	class LayoutInitializer : ILayoutUpdateStrategy
	{
		public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
		{
			LayoutAnchorablePane destPane = destinationContainer as LayoutAnchorablePane;
			if (destinationContainer != null && destinationContainer.FindParent<LayoutFloatingWindow>() != null)
				return false;

			anchorableToShow.AutoHideWidth = 200;
			anchorableToShow.AutoHideHeight = 150;
			anchorableToShow.CanShowOnHover = false;

            if (anchorableToShow.Content is SolutionViewModel || anchorableToShow.Content is ToolboxViewModel)
			{
                var explorerPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "LeftLayoutAnchorablePane");
				if (explorerPane != null)
				{
					explorerPane.Children.Add(anchorableToShow);
					return true;
				}
			}

			if (anchorableToShow.Content is PropertyViewModel || anchorableToShow.Content is NotificationViewModel)
			{
				var propertiesPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "RightLayoutAnchorablePane");
				if (propertiesPane != null)
				{
					propertiesPane.Children.Add(anchorableToShow);
					return true;
				}
			}

			if (anchorableToShow.Content is OutputViewModel || anchorableToShow.Content is DebugViewModel)
			{
				var outputPane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault(d => d.Name == "BottomLayoutAnchorablePane");
				if (outputPane != null)
				{
					outputPane.Children.Add(anchorableToShow);
					return true;
				}
			}

			return false;
		}

		public void AfterInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableShown)
		{
		}

		public bool BeforeInsertDocument(LayoutRoot layout, LayoutDocument anchorableToShow, ILayoutContainer destinationContainer)
		{
			return false;
		}

		public void AfterInsertDocument(LayoutRoot layout, LayoutDocument anchorableShown)
		{
		}
	}
}
